package com.ys.controller;

import com.github.pagehelper.PageHelper;
import com.ys.pojo.Category;
import com.ys.pojo.Company;
import com.ys.pojo.Supplier;
import com.ys.pojo.Warehouse;
import com.ys.service.*;
import com.ys.vo.PageVo;
import com.ys.vo.ResultVo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.servlet.ShiroFilter;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * 日常管理
 */
@Controller
public class RichangContorller {

    @Resource
    private CompanyService companyService;
    @Resource
    private CategoryService categoryService;
    @Resource
    private SupplierService supplierService;
    @Resource
    private WarehouseService warehouseService;
    @Resource
    private AdministratorsService aadministratorsService;
    /**
     * 添加单位
     * @param company
     * @return
     */
    @RequestMapping(value = "/adddanweis", produces = "application/json;charset=utf-8")
    @ResponseBody
    public ResultVo danwei(@RequestBody Company company){
        ResultVo resultVo = new ResultVo();
        if(companyService.addsinsert(company)){
            resultVo.setMsg("添加成功!");
        }else{
            resultVo.setMsg("添加失败!");
        }
        return resultVo;
    }

    /**
     * 查询单位
     * @return
     */
    @RequestMapping("/selectdanwei")
    @ResponseBody
    public ResultVo selectdanwei(){
        ResultVo resultVo = new ResultVo();
        resultVo.setObj(companyService.select());
        return resultVo;
    }

    /**
     * 修改单位
     * @return
     */
    @RequestMapping(value = "/updatedanwei", produces = "application/json;charset=utf-8")
    @ResponseBody
    public ResultVo updatedanwei(@RequestBody Company company){
        ResultVo resultVo = new ResultVo();
        if(companyService.updsupdate(company)){
            resultVo.setMsg("修改成功！");
        }else {
            resultVo.setMsg("修改失败！");
        }
        return resultVo;
    }

    /**
     * 查询供应商
     * @return
     */
    @RequestMapping("/selectGongYingShang")
    @ResponseBody
    public ResultVo selectGongYingShang(String companyName,String name,String phone,String pageNo,String pageSize){
        ResultVo resultVo = new ResultVo();
        //手机号  姓名 公司名称
        Supplier supplier = new Supplier();
        supplier.setCompanyName(companyName);
        supplier.setName(name);
        supplier.setPhone(phone);
        PageHelper.startPage(Integer.parseInt(pageNo),Integer.parseInt(pageSize));
        resultVo.setObj(new PageVo(supplierService.select(supplier)));
        return resultVo;
    }

    //返回
    @RequestMapping("/fanhui")
    public String fanhui(){
        return "/richang/gongyingshang";
    }

    /**
     * 修改供应商
     * @return
     */
    @RequestMapping(value = "/updateGongYingShang", produces = "application/json;charset=utf-8")
    @ResponseBody
    public ResultVo updateGongYingShang(@RequestBody Supplier supplier){
        ResultVo resultVo = new ResultVo();
        if(supplierService.updsupdate(supplier)){
            resultVo.setMsg("修改成功!");
        }else {
            resultVo.setMsg("修改失败!");
        }
        return resultVo;
    }


    /**
     * 添加供应商
     * @return
     */
    @RequestMapping(value = "/addGongYingShang", produces = "application/json;charset=utf-8")
    @ResponseBody
    public ResultVo addGongYingShang(@RequestBody Supplier supplier){
        ResultVo resultVo = new ResultVo();
        if(supplierService.addsinsertSupper(supplier)){
            resultVo.setMsg("添加成功!");
        }else {
            resultVo.setMsg("添加失败!");
        }
        return resultVo;
    }

    /**
     * 通过供应商id查询，供应商所供应的药品跳转页面
     * @return
     */
    @RequestMapping("/chaxungongyignsahngtigongdeid")
    public String chaxungongyignsahngtigongdeid(String id){
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        session.setAttribute("gongyingsahngid",id);
        return "/richang/gongyingshangs";
    }


    /**
     * 查询药品类型
     * @return
     */
    @RequestMapping("/yaopinleixing")
    @ResponseBody
    public ResultVo yaopinleixing(){
        ResultVo resultVo = new ResultVo();
        resultVo.setObj(categoryService.select());
        return resultVo;
    }

    /**
     * 添加药品类型
     * @return
     */
    @RequestMapping(value = "/addyaopinleixing", produces = "application/json;charset=utf-8")
    @ResponseBody
    public ResultVo addyaopinleixing(@RequestBody Category category){
        ResultVo resultVo = new ResultVo();
        if(categoryService.addsinsert(category)){
            resultVo.setMsg("添加成功!");
        }else {
            resultVo.setMsg("添加失败!");
        }
        return resultVo;
    }

    /**
     * 修改药品类型
     * @return
     */
    @RequestMapping(value = "/updateyaopinleixing", produces = "application/json;charset=utf-8")
    @ResponseBody
    public ResultVo updateyaopinleixing(@RequestBody Category category){
        ResultVo resultVo = new ResultVo();
        if(categoryService.updsupdate(category)){
            resultVo.setMsg("修改成功!");
        }else {
            resultVo.setMsg("修改失败!");
        }
        return resultVo;
    }




    /**
     * 添加仓库
     * @param warehouse
     * @return
     */
    @RequestMapping(value = "/addcangku", produces = "application/json;charset=utf-8")
    @ResponseBody
    public ResultVo addcangku(@RequestBody Warehouse warehouse){
        ResultVo resultVo = new ResultVo();
        if(warehouseService.addsinsertWarehouse(warehouse)){
            resultVo.setMsg("添加成功!");
        }else{
            resultVo.setMsg("添加失败!");
        }
        return resultVo;
    }

    /**
     * 查询仓库
     * @return
     */
    @RequestMapping("/selectcangku")
    @ResponseBody
    public ResultVo warehouse(String name1,String name2,String name3,String pageNo,String pageSize){
        ResultVo resultVo = new ResultVo();
        PageHelper.offsetPage(Integer.parseInt(pageNo),Integer.parseInt(pageSize));
        resultVo.setObj(new PageVo(warehouseService.selectWarehouse(name1,name2,name3)));
        return resultVo;
    }
    /**
     * 查询仓库管理员
     * @return
     */
    @RequestMapping("/selectcangkuguanliyuansss")
    @ResponseBody
    public ResultVo selectcangkuguanliyuan(){
        ResultVo resultVo = new ResultVo();
        resultVo.setObj(aadministratorsService.findAdministratorsId());
        return resultVo;
    }

    /**
     * 修改仓库
     * @return
     */
    @RequestMapping(value = "/updatecangku", produces = "application/json;charset=utf-8")
    @ResponseBody
    public ResultVo warehouse(@RequestBody Warehouse warehouse){
        ResultVo resultVo = new ResultVo();
        if(warehouseService.updsupdateWarehouse(warehouse)){
            resultVo.setMsg("修改成功！");
        }else {
            resultVo.setMsg("修改失败！");
        }
        return resultVo;
    }


}
